package com.power200.showcase.presenter;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.power200.showcase.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2015/9/24.
 */
public class GridViewPresenter {

    private int[] icons = {R.drawable.call,R.drawable.camera,R.drawable.baidumusic,R.drawable.contact,R.drawable.gallery,R.drawable.message,R.drawable.news,R.drawable.qq,R.drawable.qqmusic};
    private String[] names = {"Call","Camera","Baidu Music","Contact","Gallery","Message","News","QQ","QQMusic"};
    private List<Map<String,Object>> data = null;
    public void init(final Activity activity) {
        GridView gridView = (GridView) activity.findViewById(R.id.grid);
        SimpleAdapter adapter = new SimpleAdapter(activity, getData(), R.layout.grid_item, new String[]{"image", "text"}, new int[]{R.id.gImageView, R.id.gTextView});
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String,Object> item = (Map<String, Object>) parent.getItemAtPosition(position);
                Toast.makeText(activity,item.get("text").toString(),Toast.LENGTH_LONG).show();
            }
        });
    }

    private List<Map<String,Object>> getData() {
        if(data == null) {
            data = new ArrayList<Map<String,Object>>();
            for (int i = 0; i < icons.length;i++) {
                Map<String,Object> each = new HashMap<String,Object>();
                each.put("image",icons[i]);
                each.put("text",names[i]);
                data.add(each);
            }
        }
        return data;
    }
}
